﻿//********************************************************************************//
//创建日期：<创建日期，2016-09-12>
//创建作者：<孙雪婷，sunxueting@shanghai3h.com>
//功能说明：表信息维护
//********************************************************************************//

using System;
using System.Data;
using System.Web.UI;
using BLL;
using BLL.Access;
using Entity;
using Web.Class;

namespace Web.Pages.QuShui.DiXiaS._2.YongHuGL
{
    public partial class BiaoXinXWH : BasePage
    {
        #region 属性

        /// <summary>
        ///     gridView主键(用户信息ID)
        /// </summary>
        private int BIAOXXID
        {
            get { return Convert.ToInt32(ViewState["_BIAOXXID"]); }
            set { ViewState["_BIAOXXID"] = value; }
        }

        /// <summary>
        ///     保存结果集
        /// </summary>
        private DataTable dtSource
        {
            get { return ViewState["dtSource"] as DataTable; }
            set { ViewState["dtSource"] = value; }
        }

        #endregion

        #region 变量

        /// <summary>
        ///     用户信息BLL
        /// </summary>
        private readonly QS_YG_YONGHUXXBLL _QS_YG_YONGHUXXBLL = new QS_YG_YONGHUXXBLL();

        /// <summary>
        ///     深井信息BLL
        /// </summary>
        private readonly QS_YH_SHENJINGXXBBLL _SJXXBLL = new QS_YH_SHENJINGXXBBLL();

        /// <summary>
        ///     水表信息
        /// </summary>
        private QS_YH_SHUIBIAOXXB _SBXX = new QS_YH_SHUIBIAOXXB();

        /// <summary>
        ///     水表信息bll
        /// </summary>
        private readonly QS_YH_SHUIBIAOXXBBLL _SBXXBLL = new QS_YH_SHUIBIAOXXBBLL();

        /// <summary>
        ///     日志BLL
        /// </summary>
        private readonly SYS_LOGBLL _SYS_LOGBLL = new SYS_LOGBLL();

        #endregion

        #region 事件

        /// <summary>
        ///     页面加载事件
        /// </summary>
        protected void Page_Load(object sender, EventArgs e)
        {
            //第一次页面加载
            if (!IsPostBack)
            {
                InitControl();
                InitData();
                Display();
            }
        }

        /// <summary>
        ///     重写Render方法
        /// </summary>
        protected override void Render(HtmlTextWriter sender)
        {
            base.Render(sender);
        }


        /// <summary>
        ///     放弃
        /// </summary>
        protected void btnCancel_Click(object sender, EventArgs e)
        {
            Status = GridViewStatus.Init;
            Display();
            //如果当前有操作项 重新加载数据
            if (BIAOXXID > 0)
                SetData();
        }

        /// <summary>
        ///     修改
        /// </summary>
        protected void btnEdit_Click(object sender, EventArgs e)
        {
            if (txtShuiBiaoBH.Text == "")
            {
                PageTools.AlertMsg(this, "水表编号为空,请输入！");
            }
            else
            {
                var dt = _SBXXBLL.SelectQS_YH_SHUIBIAOXXBsDynamic(
                    string.Format("I_JLZT=0 and S_SHUIBIAOBH='{0}' ", txtShuiBiaoBH.Text.Trim()), null);
                if (dt == null || dt.Rows.Count < 1)
                {
                    BIAOXXID = -1;
                    PageTools.AlertMsg(this, "该水表号不存在,请重新输入！");
                }
                else
                {
                    BIAOXXID = Convert.ToInt32(dt.Rows[0]["ID"]);
                    Status = GridViewStatus.Update;
                    Display();
                    SetData();
                }
            }
        }

        /// <summary>
        ///     保存
        /// </summary>
        protected void btnSave_Click(object sender, EventArgs e)
        {
            if (ValidateData())
                SaveData();
        }

        #endregion

        #region 方法

        /// <summary>
        ///     初始化控件
        /// </summary>
        private void InitControl()
        {
            txtYongHuH.Text = "";
            txtYONGHUM.Text = "";
            txtJingBH.Text = "";
            txtQuShuiDD.Text = "";
            txtYuanShuiBH.Text = "";
            txtBiaoHao.Text = "";
            txtBiaoJing.Text = "";
            cmbCengHao.Text = "";
            cmbShuiLiangLB.Text = "";
            cmbBiaoFenL.Text = "";
            cmbBiaoBiaoJ.Text = "";
            BeiZhu.Text = "";
        }

        /// <summary>
        ///     初始化数据
        /// </summary>
        private void InitData()
        {
            var wtype = BusinessTool.WType;
            if (wtype == 1)
                jbh.InnerText = "取水口编号：";
            else
                jbh.InnerText = "井编号：";

            //加载册号
            LoadCH();
            //加载水量类别
            LoadSLLB();
            //加载表分类
            LoadBFL();
            var sbh = Request.QueryString["sbh"];
            if (sbh != "" && sbh != null)
            {
                txtShuiBiaoBH.Text = sbh;
                btnEdit_Click(null, null);
            }
        }

        /// <summary>
        ///     加载册号
        /// </summary>
        private void LoadCH()
        {
            var wtype = BusinessTool.WType;
            string[] arr1 =
            {
                "001", "002", "003", "005", "008", "009", "101", "106", "111", "113", "115", "204", "207", "208", "209",
                "210", "213", "214", "216", "217", "218"
            };
            string[] arr2 = {"600"};
            var arr = wtype == 1 ? arr2 : arr1;

            for (var i = 0; i < arr.Length; i++)
                cmbCengHao.Items.Add(arr[i], arr[i]);
            if (wtype == 1)
                cmbCengHao.SelectedIndex = 0;
        }

        /// <summary>
        ///     加载水量类别
        /// </summary>
        private void LoadSLLB()
        {
            //1	普通采
            //2	采灌采
            //3	采灌灌
            //4	矿泉采
            //5	专灌
            var wtype = BusinessTool.WType;
            if (wtype == 1)
            {
                cmbShuiLiangLB.ClientEnabled = false;
            }
            else
            {
                cmbShuiLiangLB.Items.Clear();
                cmbShuiLiangLB.ClientEnabled = true;
                cmbShuiLiangLB.Items.Add("普通采", "1");
                cmbShuiLiangLB.Items.Add("采灌采", "2");
                cmbShuiLiangLB.Items.Add("采灌灌", "3");
                cmbShuiLiangLB.Items.Add("矿泉采", "4");
                cmbShuiLiangLB.Items.Add("专灌", "5");
            }
        }

        /// <summary>
        ///     加载表分类
        /// </summary>
        private void LoadBFL()
        {
            cmbBiaoFenL.Items.Add("总表", "1");
            cmbBiaoFenL.Items.Add("分表", "2");
            cmbBiaoFenL.Items.Add("虚拟表", "3");
            //DataTable dt = SLFP.SelectQS_JX_SHUILIFPXXesDynamic("I_JLZT=0", null);
            //for (int i = 0; i < dt.Rows.Count; i++)
            //{
            //    //cmbShuiLiFP.Items.Add(dt.Rows[i]["S_SHUILIFPMC"].ToString(), dt.Rows[i]["S_SHUILIFPBH"]);
            //}
        }


        /// <summary>
        ///     数据加载
        /// </summary>
        private void LoadData()
        {
        }

        /// <summary>
        ///     用于控制添加、修改、查看三种状态的显示
        /// </summary>
        private void Display()
        {
            switch (Status)
            {
                case GridViewStatus.Init:
                    btnEdit.Visible = true;
                    btnEdit.Enabled = true;
                    btnSave.Visible = false;
                    btnCancel.Visible = false;
                    txtShuiBiaoBH.ClientEnabled = true;
                    txtShuiBiaoBH.Focus();
                    InitControl();
                    SetText(false);
                    break;
                case GridViewStatus.Update:
                    btnEdit.Visible = false;
                    btnSave.Visible = true;
                    btnCancel.Visible = true;
                    txtShuiBiaoBH.ClientEnabled = false;
                    SetText(true);
                    break;
                case GridViewStatus.Add:
                    btnEdit.Visible = false;
                    btnSave.Visible = true;
                    btnCancel.Visible = true;
                    SetText(true);
                    break;
            }
        }


        /// <summary>
        ///     设置输入框状态
        /// </summary>
        /// <param name="b"></param>
        private void SetText(bool b)
        {
            cmbCengHao.ClientEnabled = b;
            cmbShuiLiangLB.ClientEnabled = b;
            cmbBiaoFenL.ClientEnabled = b;
            cmbBiaoBiaoJ.ClientEnabled = b;
            BeiZhu.ClientEnabled = b;
        }

        /// <summary>
        ///     保存数据
        /// </summary>
        private void SaveData()
        {
            OperateState operateState;
            //修改操作
            operateState = OperateState.Modify;
            /*var type = Session["WType"] as string;
            if (new SqlHelper().P_QS_INSERT_SHUIBIAO_HIS(BIAOXXID, LoginInfo.CurrentUser.UserID.ToString(),
                type, null))
            {*/
            SpHelper _spHelper = new SpHelper();
            int? resmsg = 0;
            _spHelper.P_QSYH_YZXX(BusinessTool.WType, 4, this.txtShuiBiaoBH.Text, out resmsg);
            if (resmsg == 0)
            {
                _spHelper.P_QS_INSERT_SHUIBIAO_HIS(this.txtShuiBiaoBH.Text, LoginInfo.CurrentUser.UserID.ToString(), BusinessTool.WType.ToString());
            }
            //记录ID 只是用于记录修改日志
            _SBXX = _SBXXBLL.SelectQS_YH_SHUIBIAOXXB(BIAOXXID);
            _SBXX.S_CEHAO = cmbCengHao.Value.ToString();
            _SBXX.S_SHUILIANGLB = cmbShuiLiangLB.Value.ToString();
            _SBXX.S_BIAOFENL = cmbBiaoFenL.Value.ToString();
            _SBXX.S_BIAOBIAOJ = cmbBiaoBiaoJ.Value.ToString();
            _SBXX.S_BEIZHU = BeiZhu.Text;

            _SYS_LOGBLL.Log(LoginInfo.CurrentUser, BusinessTool.WType.ToString(), "用户管理-表信息维护", operateState, _SBXX,
                null);
            var result = _SBXXBLL.InsertOrUpdate(_SBXX);
            if (result)
            {
                PageTools.AlertMsg(this, "保存成功！");
                Status = GridViewStatus.Init;
                Display();
            }
            else
            {
                PageTools.AlertMsg(this, "保存失败！");
            }
            /*}
            else
            {
                PageTools.AlertMsg(this, "保存失败！");
            }*/

        }

        /// <summary>
        ///     给输入框赋值
        /// </summary>
        private void SetData()
        {
            _SBXX = _SBXXBLL.SelectQS_YH_SHUIBIAOXXB(BIAOXXID);
            var dtyh = _QS_YG_YONGHUXXBLL.SelectQS_YG_YONGHUXXesDynamic(
                string.Format(
                    "s_yonghuh =(select s_yonghuh from QS_YH_YONGHUSBGX where S_SHUIBIAOH='{0}' and i_jlzt=0) and i_jlzt=0",
                    txtShuiBiaoBH.Text), null);
            txtYongHuH.Text = dtyh.Rows.Count > 0 ? dtyh.Rows[0]["S_YONGHUH"].ToString() : "";
            txtYONGHUM.Text = dtyh.Rows.Count > 0 ? dtyh.Rows[0]["S_YONGHUM"].ToString() : "";
            txtJingBH.Text = _SBXX.S_QUSHUIK;
            var dt = _SJXXBLL.SelectQS_YH_SHENJINGXXBsDynamic(
                string.Format("s_jingbh='{0}' and i_jlzt=0", _SBXX.S_QUSHUIK), null);
            txtQuShuiDD.Text = dt.Rows.Count > 0 ? dt.Rows[0]["S_JINGDIZ"].ToString() : "";
            txtYuanShuiBH.Text = _SBXX.S_YUANSHUIBH;
            txtBiaoHao.Text = _SBXX.S_BIAOHAO;
            txtBiaoJing.Text = "";
            cmbCengHao.Value = _SBXX.S_CEHAO;
            cmbShuiLiangLB.Value = _SBXX.S_SHUILIANGLB;
            cmbBiaoFenL.Value = _SBXX.S_BIAOFENL;
            cmbBiaoBiaoJ.Value = _SBXX.S_BIAOBIAOJ;
            txtZongBiaoH.Text = _SBXX.S_ZONGBIAOH;
            BeiZhu.Text = _SBXX.S_BEIZHU;
        }

        /// <summary>
        ///     保存验证
        /// </summary>
        /// <returns></returns>
        private bool ValidateData()
        {
            if (cmbCengHao.Text == "")
            {
                PageTools.AlertMsg(this, "册号不能为空！");
                cmbCengHao.Focus();
                return false;
            }
            if (cmbShuiLiangLB.Text == "")
            {
                PageTools.AlertMsg(this, "水量类别不能为空！");
                cmbShuiLiangLB.Focus();
                return false;
            }
            if (cmbBiaoFenL.Text == "")
            {
                PageTools.AlertMsg(this, "表分类不能为空！");
                cmbBiaoFenL.Focus();
                return false;
            }

            return true;
        }

        #endregion
    }
}